//渲染菜单
var Menu = function (treeId) {
    this.treeId = treeId;
    this.treeData = null;
    this.treeObj = null;
    this.treeSetting = {
        "view": {
            "addDiyDom": addDiyDom,
            "addHoverDom": addHoverDom,
            "removeHoverDom": removeHoverDom
        },
        "data": {
            "key": {
                "url": "noJump"
            }
        }
    };

    this.hasInit = false;

    this.renderMenu = function () {
        let that = this;
        curl.get("/admin/authority/menu/tree", "", true, function (res) {
            if (res.result === "success") {
                that.treeData = res.data;
                //生成菜单
                let zTreeObj = $.fn.zTree.init($('#' + that.treeId), that.treeSetting, res.data);
                if (!that.hasInit) {    //第一次渲染页面时绑定zTree对象
                    that.treeObj = zTreeObj;
                    that.hasInit = true;
                }
            } else {
                layer.alert(res.message, {icon: 5});
            }
        }, function (error) {
            layer.alert(error, {icon: 5});
        });
    }


    this.addMenuNode = function (data, next) {
        if (!this.checkData(data)) {
            return;
        }
        curl.post("/admin/authority/menu/add", data, 1, true, function (res) {
            if (res.result === "success") {
                next()
            } else {
                layer.alert(res.message, {icon: 5});
            }
        }, function (msg) {
            layer.alert(msg, {icon: 5});
        });
    }

    this.getMenuNode = function (id, next) {
        curl.get("/admin/authority/menu/get/" + id, 2, true, function (res) {
            if (res.result === "success") {
                next(res.data)
            } else {
                layer.alert(res.message, {icon: 5});
            }
        }, function (msg) {
            layer.alert(msg, {icon: 5});
        });
    }

    this.updateMenuNode = function (data, next) {
        if (!this.checkData(data)) {
            return;
        }
        curl.post("/admin/authority/menu/update", data, 1, true, function (res) {
            if (res.result === "success") {
                next()
            } else {
                layer.alert(res.message, {icon: 5});
            }
        }, function (msg) {
            layer.alert(msg, {icon: 5});
        });
    }

    this.removeMenuNode = function (id, next) {
        curl.post("/admin/authority/menu/delete", {id: id}, 2, true, function (res) {
            if (res.result === "success") {
                next()
            } else {
                layer.alert(res.message, {icon: 5});
            }
        }, function (msg) {
            layer.alert(msg, {icon: 5});
        });
    }

    this.checkData = function (data) {
        for (const key in data) {
            if (data[key] === undefined || data[key] === null) {
                layer.alert(key + "不能为空！", {icon: 3});
                return false;
            }
        }

        return true;
    }
};

function addDiyDom(treeId, treeNode) {
    let spanId = treeNode.tId + '_ico';
    $('#' + spanId).removeClass().addClass(treeNode.icon);
}

function addHoverDom(treeId, treeNode) {
    let aObj = $("#" + treeNode.tId + "_a"); // tId = permissionTree_1, ==> $("#permissionTree_1_a")
    //aObj.attr("href", "javascript:;");
    let id = treeNode.id;

    if (treeNode.editNameFlag || $("#btnGroup" + treeNode.tId).length > 0) return;
    let s = '<span id="btnGroup' + treeNode.tId + '">';
    if (treeNode.level === 0) {
        //根仅允许添加子菜单
        s += '<a class="btn btn-info dropdown-toggle btn-xs menu-btn-add" data-id="' + id + '" style="margin-left:10px;padding-top:0px;" href="javascript:;" >&nbsp;&nbsp;<i class="fa fa-fw fa-plus rbg "></i></a>';
    } else if (treeNode.level === 1) {
        //一级允许修改和添加
        s += '<a class="btn btn-info dropdown-toggle btn-xs menu-btn-edit" data-id="' + id + '" style="margin-left:10px;padding-top:0px;"  href="javascript:;" title="一级菜单">&nbsp;&nbsp;<i class="fa fa-fw fa-edit rbg "></i></a>';
        if (treeNode.children.length === 0) {   //没有子节点允许删除
            s += '<a class="btn btn-info dropdown-toggle btn-xs menu-btn-remove" data-id="' + id + '" style="margin-left:10px;padding-top:0px;" href="javascript:;" >&nbsp;&nbsp;<i class="fa fa-fw fa-times rbg "></i></a>';
        }
        s += '<a class="btn btn-info dropdown-toggle btn-xs menu-btn-add" data-id="' + id + '" style="margin-left:10px;padding-top:0px;" href="javascript:;" >&nbsp;&nbsp;<i class="fa fa-fw fa-plus rbg "></i></a>';
    } else if (treeNode.level === 2) {
        s += '<a class="btn btn-info dropdown-toggle btn-xs menu-btn-edit" data-id="' + id + '" style="margin-left:10px;padding-top:0px;"  href="javascript:;" title="二级菜单">&nbsp;&nbsp;<i class="fa fa-fw fa-edit rbg "></i></a>';
        s += '<a class="btn btn-info dropdown-toggle btn-xs menu-btn-remove" data-id="' + id + '" style="margin-left:10px;padding-top:0px;" href="javascript:;">&nbsp;&nbsp;<i class="fa fa-fw fa-times rbg "></i></a>';
    }

    s += '</span>';
    aObj.after(s);
}

function removeHoverDom(treeId, treeNode) {
    $("#btnGroup" + treeNode.tId).remove();
}